// pages/Authentication/Authentication.js
var call = require("../../utils/ajax.js")
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    URL:"",
    // token
    tokenID: "",
    //联盟唯一id
    organization_id: "",
    // 联盟信息
    union: {},
    mm:"",
    //按钮提示
    btn_ent: false,
    //图片地址：
    imgAdress: "../../images/dog.jpg",
    organization_name:"",
    organization_category:"",
    organization_city_area:"",
    organization_big_area:"",
    organization_contact:"",
    organization_wechat:"",
    organization_message:"",
    organization_big_area:"",
    organization_index:"",
    organization_picture:"",
    organization_county_area:"",
    fileName:"",
    path:""

  },
  //修改按钮点击事件

  modUnion: function(e) {
    var da = this.data.union
    if (da.organization_name == "" || da.organization_category == "" ||
      da.organization_city_area == "" || da.organization_big_area == "" ||
      da.organization_contact == "" || da.organization_wechat == "" || da.organization_message == "" || da.organization_big_area == ""
    ) {
      this.data.btn_ent = true;
      wx.showModal({
        title: '提示',
        content: '请完善数据',
        success: function(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else {
            console.log('用户点击取消')
          }

        }
      })
    } else {
      //进行修改
      var that = this;
      let message = this.getM();
      let make = "";
      make = wx.getStorageSync('tokenID');
      console.log("修改联盟信息");
      let url = "Organization/putOrganiztaion";
      call.passRequest1(url,message,function (res){
        console.log(res)
        // wx.navigateBack({
        //   changed: true
        // }); //返回上一页 
        wx.showToast({
          title: '修改成功',
        })
      },function (e){
        
      })
 
      console.log("数据无空值");
    }
  },
  //上传图片
  uploadImg: function() {
    var that = this;
    console.log(that.data.imgAdress[0]);
  },
  //获取头像
  getimg: function() {
    var that = this;
    var hh = this.data.union;
    console.log(hh)
    wx.chooseImage({
      count: 1, // 默认9 
      success: function(res) {
        // that.setData({
        //   imgAdress: res.tempFilePaths
        // })
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        console.log(tempFilePaths);
        let fileName  = hh.organization_index;
        let url = 'Organization/addOrganImage?className=logo&';
      if(hh.organization_index!=undefined&&hh.organization_index!=""){
        url=url+"fileName="+fileName+"&"
       }
        call.up(url,tempFilePaths[0],function (res) {
          res = JSON.parse(res)
          console.log(res.date)
          wx.setStorageSync("path",res.date.path)
          let kk = wx.getStorageSync("path");
          console.log(kk)
          console.log(kk)
          that.setData({
            imgAdress:tempFilePaths[0]
          })
          
        },function (params) {
          
        })
      },
    })
  },
  //input输入框数据
  organization_name: function(e) {
    this.setData({
      organization_name:e.detail.value
    })
  },
  organization_category: function(e) {
    this.setData({
      organization_category:e.detail.value
    })
  },
  organization_city_area: function(e) {
    this.setData({
      organization_city_area:e.detail.value
    })
  },
  organization_contact: function(e) {
    this.setData({
      organization_contact:e.detail.value
    })
   
  },
  organization_wechat: function(e) {
    this.setData({
      organization_wechat:e.detail.value
    })
  
  },
  organization_message: function(e) {
    this.setData({
      organization_message:e.detail.value
    })
    
  },
  organization_county_area: function(e) {
    this.setData({
      organization_county_area:e.detail.value
    })
  },
  getM:function (){
    let union = {
    "organization_big_area": "详细位置",
    // "organization_category": "救助机构",
    // "organization_city_area": "市级地址",
    // "organization_contact": "手机号",//手机号码必填
    // "organization_county_area": "机构县市区地址",
    // "organization_index": "1177871321542496256",//这里填写fileName
    // "organization_message": "简介",
    // "organization_name": "福建动物保护协会",
    "organization_picture": "string/Logo/1177871321542496256/1177871321655742464.png",//此处为路径
    // "organization_wechat": "微信"
  };

  
  console.log(union);
    union = this.data.union;
    if(this.data.organization_name!=undefined&&this.data.organization_name!="")
    union.organization_name = this.data.organization_name;
    if(this.data.organization_category!=undefined&&this.data.organization_category!="")
    union.organization_category = this.data.organization_category;
    if(this.data.organization_city_area!=undefined&&this.data.organization_city_area!="")
    union.organization_city_area = this.data.organization_city_area;
    if(this.data.organization_county_area!=undefined&&this.data.organization_county_area!="")
    union.organization_county_area = this.data.organization_county_area;
    if(this.data.organization_contact!=undefined&&this.data.organization_contact!="")
    union.organization_contact = this.data.organization_contact;
    if(this.data.organization_wechat!=undefined&&this.data.organization_wechat!="")
    union.organization_wechat = this.data.organization_wechat;
    if(this.data.organization_message!=undefined&&this.data.organization_message!="")
    union.organization_name = this.data.organization_name;
    if(this.data.organization_big_area!=undefined&&this.data.organization_big_area!="")
    union.organization_big_area = this.data.organization_big_area;
    let kk = wx.getStorageSync("path");
    console.log(kk)
    if(kk!=undefined&&kk!="")
    union.organization_picture = kk;
    if(this.data.fileName!=undefined&&this.data.fileName!="")
    union.organization_index = this.data.fileName;
    // console.log(this.data.mm)
    console.log(union);
    return union;
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
                      
            this.setData({
                  URL:app.globalData.URL
              })
   this.union(options.id);
  },
  //加载联盟信息
  union(id) {
    var that = this;
    console.log("=====token=====" + id);
    console.log("加载联盟信息");
    let url = 'Organization/getOrganization/' + id;
    call.getData1(url,function (res){
      console.log("-=-=-=-=");
      console.log(res);
      
      let L = that.data.URL+res.date.organization.organization_picture
      that.setData({
        union: res.date.organization,
        mm: res.date.organization,
        imgAdress:L
      })
      wx.setStorageSync("path",res.date.organization_picture)
      let kk = wx.getStorageSync("path");
      console.log(kk)
      that.getM()
      console.log(that.getM());
    })
    console.log()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function(options) {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})